home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
MacHack 1997
/
MacHack 1997.toast
/
Hacks
/
Hacks ’96
/
ABC Hack
/
source
/
SaneNumerics.c
< prev
next >
Wrap
Text File
|
1996-06-22
|
3KB
|
134 lines
// SaneNumerics 6/21/96
// by Nathan Tennies, John Gotow, and Jeff
//
// SaneNumerics builds a code resource that can be appended to an 'itl2' resource to change the normal
// sort ordering of text through the Internation Utilities. SaneNumerics sorts numbers embedded in
// text in the natural way, with '3' coming before '21', rather than alphabetically. SaneNumerics can
// be changed - by changing the ComparePartialStrings function, to sort text in other ways.
#include "SaneNumerics.h"
void main (void) {
asm {
bra Init
bra Fetch
dc.l 'NSrt' // OSType identifier
dc.l 0 // OldInit offset 8
dc.l 0 // OldFetch offset 12
dc.l 0 // OldResourceSize offset 16
}
}
void Init (void) {
asm {
move.w #0, STACKOFFSET(IUSortFrame, oldA6, flag) (a6);
lea main,a0
move.l 20(a0),d1
move.l 12(a0), d0
sub.l d1,a0
add.l d0,a0
jsr (a0)
}
}
void Fetch (void) {
asm {
tst.w STACKOFFSET(IUSortFrame, oldA6, flag)(a6)
bne @secondStr
move.l IUStrData.strPtr (a2), STACKOFFSET(IUSortFrame, oldA6, remStrA) (a6)
move.w IUStrData.strCnt (a2), STACKOFFSET(IUSortFrame, oldA6, remLenA) (a6)
move.w #1, STACKOFFSET(IUSortFrame, oldA6, flag) (a6)
lea main,a0
move.l 20(a0),d1
move.l 16(a0), d0
sub.l d1,a0
add.l d0,a0
jsr (a0)
rts
@secondStr:
move.w IUStrData.strCnt (a2), d0
add.w #1, d0
move.w d0,-(a7)
move.w STACKOFFSET(IUSortFrame, oldA6, remLenA) (a6), d0
add.w #1, d0
move.w d0,-(a7)
move.l IUStrData.strPtr (a2), -(a7)
move.l STACKOFFSET(IUSortFrame, oldA6, remStrA) (a6), -(a7)
lea STACKOFFSET(IUSortFrame, oldA6, remStrA) (a6), a0
move.l a0, -(a7)
lea main,a0
move.l 20(a0),d1
move.l 16(a0), d0
sub.l d1,a0
add.l d0,a0
jsr (a0)
jsr ComparePartialStrings
add.l #16,a7
tst.w d0
beq @oldFetch
cmp.w #1, d0
beq @strAGreater
move.w #0xFF00, d4
rts
@strAGreater:
move.w #0x0000, d4
rts
@oldFetch:
lea main,a0
move.l 20(a0),d1
move.l 16(a0), d0
sub.l d1,a0
add.l d0,a0
jsr (a0)
rts
}
}
short RunLen (char *ptr, short remainingLen) {
short runLen = 0;
while ((remainingLen > 0) && (*ptr >= '0') && (*ptr <= '9')) {
++runLen;
++ptr;
--remainingLen;
}
return runLen;
}
// ComparePartialStrings accepts two partial strings, remStrA and remStrB, of lengths remLenA and remLenB.
// This function should return 1 if remStrA is greater than remStrB, -1 if remStrA is less than remStrB,
// and 0 if no changes to the default sort order are to be made. sortFrame contains additional information,
// including the whole size of the strings.
short ComparePartialStrings (IUSortFrame *sortFrame, char * remStrA, char * remStrB, short remLenA, short remLenB) {
short compareResult = 0;
short digitRunLenA = RunLen (remStrA, remLenA);
short digitRunLenB = RunLen (remStrB, remLenB);
if ((digitRunLenA > 0) && (digitRunLenB > 0) && (digitRunLenA != digitRunLenB)) {
if (digitRunLenA < digitRunLenB)
compareResult = -1;
else
compareResult = 1;
}
return compareResult;
}